package com.projles.ttd;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.projles.ttd.util.Player;

public class OpcoesActivity extends Activity implements OnClickListener{
	
	Button btSalvar;
	Button btCancelar;
	CheckBox cbMudo;
	CheckBox cbVibra;
	
	Player player;
	
	private SharedPreferences config;
	
	public static final String PREFS_NAME = "preferencias";
	
	public static final String MUTE = "mute";
	public static final String VIBRA = "vibra";
	
	private EditText etNumRodadas;
	private static int numRodadas;
	private final int NUM_RODADAS_MINIMA = 1;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.opcoes);
        
        config = getSharedPreferences(PREFS_NAME, 0);
        
        btSalvar = (Button) findViewById(R.id.bt_salvar_opcoes);
        btSalvar.setOnClickListener(this);      

        btCancelar = (Button) findViewById(R.id.bt_cancelar_opcoes);
        btCancelar.setOnClickListener(this);
        
        etNumRodadas = (EditText) findViewById(R.id.et_tempo_cronometro);
        int valor_atual = config.getInt("num_rodadas", 3);
        etNumRodadas.setText(Integer.toString(valor_atual)); //setar valor atual do num de rodadas
        etNumRodadas.selectAll();
        
        cbMudo = (CheckBox) findViewById(R.id.cb_mudo);
        cbMudo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.click();
            }
        });
        cbVibra = (CheckBox) findViewById(R.id.cb_vibra);
        cbVibra.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.click();
            }
        });
        
        player = Player.getPlayer(this);
        
        cbMudo.setChecked(player.isMuted());
        cbVibra.setChecked(player.isVibra());
    }
    
    public void salvar (){
    	player.setMute(cbMudo.isChecked());
    	player.setVibra(cbVibra.isChecked());
    	
    	String numRodadasStr = etNumRodadas.getText().toString().trim();
    	
    	try {
    		numRodadas = numRodadasStr.length() != 0 ? Integer.parseInt(numRodadasStr) : NUM_RODADAS_MINIMA;
    	} catch (Exception e) {
			return;
		}
    	
    	if (numRodadas < NUM_RODADAS_MINIMA) {
    		numRodadas = NUM_RODADAS_MINIMA;
    	}
    	
    	salvarOpcoes(player, config);
    	
    	this.setResult(RESULT_OK);
    	finish();
    }
    
    
    public static void salvarOpcoes(Player player, SharedPreferences config) {
    	
    	SharedPreferences.Editor editor = config.edit();
    	
    	editor.putInt("num_rodadas", numRodadas);
    	editor.putBoolean(MUTE, player.isMuted());
    	editor.putBoolean(VIBRA , player.isVibra());
    	
    	editor.commit();
    }
    

	@Override
	public void onClick(View v) {
		
		player.click();
		switch (v.getId()) {
		case R.id.bt_salvar_opcoes:
			salvar();						
			break;

		case R.id.bt_cancelar_opcoes:
			finish();
			break;
		
		default:
			break;
		}
	}


}
